home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1996
/
MacHack 1996.toast
/
Hacks
/
Hacks ’92
/
Text Capture FKEY
/
Select_rect.c
< prev
next >
Wrap
Text File
|
1992-03-15
|
2KB
|
99 lines
void Select_rect( Rect *rect );
/*
Select a rectangle from the screen, in global coordinates.
*/
static void Make_rgn( Point one, Point two, RgnHandle new_rgn );
static Pattern my_gray ={0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55};
/* ------------------------ Select_rect ----------------------------- */
void Select_rect( Rect *rect )
{
GrafPtr save_port;
GrafPort wide_open;
CursHandle cross_hair_h;
Cursor crosshair;
EventRecord event;
RgnHandle old_rgn, new_rgn, diff_rgn;
Point anchor_pt, old_pt, new_pt;
old_rgn = NewRgn();
new_rgn = NewRgn();
diff_rgn = NewRgn();
/*
Create a wide open GrafPort in which to draw on all screens.
*/
GetPort( &save_port );
OpenPort( &wide_open );
CopyRgn( GetGrayRgn(), wide_open.visRgn );
wide_open.portRect = (**wide_open.visRgn).rgnBBox;
PenMode( patXor );
PenPat( my_gray );
cross_hair_h = GetCursor( crossCursor );
crosshair = **cross_hair_h;
InitCursor(); // make sure the cursor is visible
SetCursor( &crosshair );
/*
I use GetOSEvent instead of GetNextEvent so that I won't have to
worry about the mouse click causing a process switch.
*/
while (!GetOSEvent( mDownMask, &event )) // wait for a mouse click
{
SetCursor( &crosshair );
}
GetMouse( &anchor_pt ); // this is one corner of the rectangle
old_pt = anchor_pt;
Make_rgn( anchor_pt, old_pt, old_rgn );
while (StillDown()) // track the mouse
{
GetMouse( &new_pt );
if (!EqualPt( new_pt, old_pt ))
{
Make_rgn( anchor_pt, new_pt, new_rgn );
XorRgn( old_rgn, new_rgn, diff_rgn );
PaintRgn( diff_rgn ); // in patXor mode
old_pt = new_pt;
CopyRgn( new_rgn, old_rgn );
}
}
PaintRgn( new_rgn ); // erase the rectangle
*rect = (**new_rgn).rgnBBox; // and return it
SetPort( save_port );
ClosePort( &wide_open );
InitCursor();
DisposeRgn( new_rgn );
DisposeRgn( old_rgn );
DisposeRgn( diff_rgn );
}
/* ------------------------ Make_rgn -------------------------------- */
/*
Make a region that is a one pixel thick rectangular frame,
given two corners.
*/
static void Make_rgn( Point anchor, Point cursor, RgnHandle new_rgn )
{
Rect work_rect;
RgnHandle work_rgn = NewRgn();
if (cursor.h > anchor.h) ++cursor.h;
if (cursor.v > anchor.v) ++cursor.v;
Pt2Rect( anchor, cursor, &work_rect );
RectRgn( new_rgn, &work_rect );
InsetRect( &work_rect, 1, 1 );
RectRgn( work_rgn, &work_rect );
DiffRgn( new_rgn, work_rgn, new_rgn );
DisposeRgn( work_rgn );
}